package com.emagic.manage.common.ui.base;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.emagic.manage.R;
import com.emagic.manage.common.consts.SysParams;
import com.emagic.manage.data.exception.BizException;
import com.emagic.manage.data.exception.ServerError;
import com.emagic.manage.mvp.views.LoadDataView;
import com.emagic.manage.utils.ToastUtil;
import com.xitaiinfo.library.compat.errorview.ErrorView;
import com.xitaiinfo.library.compat.widget.BetterViewAnimator;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import retrofit2.adapter.rxjava.HttpException;

/**
 * 提供视图状态切换的Activity 基类
 *
 * @author baoy
 * @version 1.0
 *          Create by 2017/7/13 下午1:23
 */
public class AppStateActivity extends BaseActivity implements LoadDataView {

    /*base view*/
    private BetterViewAnimator mViewAnimator;
    private FrameLayout mContentView;
    private CoordinatorLayout mBaseView;
    private ErrorView mErrorView;

    private static int[] ATTRS = {
            R.attr.windowActionBarOverlay,
            R.attr.actionBarSize
    };

    private void initContentView() {
        mBaseView = (CoordinatorLayout) getLayoutInflater().inflate(R.layout.activity_base, null);
        mViewAnimator = (BetterViewAnimator) mBaseView.findViewById(R.id.view_animator);
        mContentView = (FrameLayout) mBaseView.findViewById(R.id.content_view);
        mErrorView = (ErrorView) mBaseView.findViewById(R.id.error_view);
    }

    @SuppressWarnings("ResourceType")
    private void initChildView(int layoutResID) {
        View mChildView = getLayoutInflater().inflate(layoutResID, null);
        CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(ATTRS);
        //获取主题中定义的悬浮标志
        boolean overly = typedArray.getBoolean(0, false);
        //获取主题中定义的toolbar的高度
        int toolBarSize = (int) typedArray.getDimension(1, (int) getContext().getResources().getDimension(R.dimen.abc_action_bar_default_height_material));
        typedArray.recycle();
        //如果是悬浮状态，则不需要设置间距
//        params.topMargin = overly ? 0 : toolBarSize;
        mContentView.addView(mChildView);
        mViewAnimator.setLayoutParams(params);

    }

    protected ViewGroup getRootView() {
        return this.mBaseView;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initContentView();
    }

    @Override
    public Context getContext() {
        return AppStateActivity.this;
    }


    @Override
    public void showError(String message) {
//        Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
        ToastUtil.showMiddleScreenToast(getContext(), message);
    }

    @Override
    public void setContentView(int layoutResID) {
        //初始化子类布局
        initChildView(layoutResID);
        //设置当前布局
        setContentView(mBaseView);
    }

    @Override
    public void showLoadingView() {
        mViewAnimator.setDisplayedChildId(R.id.loading_view);
    }

    @Override
    public void showEmptyView(ErrorView.Config config, ErrorView.OnRetryListener onRetryListener) {

        ErrorViewFactory.buildEmptyErrorView(mErrorView, config, onRetryListener);
        mViewAnimator.setDisplayedChildId(R.id.error_view);
    }

    @Override
    public void showErrorView(Throwable throwable, ErrorView.Config config, ErrorView.OnRetryListener onRetryListener) {

        if (throwable != null) {
            //log错误文本
            Log.e(SysParams.ERROR_TAG, "showErrorView: " + throwable.getMessage(), throwable);
        }
        if (throwable != null && throwable instanceof BizException) {
            ErrorViewFactory.buildLoadFailErrorView(mErrorView, config, onRetryListener);
        } else if (throwable != null && throwable instanceof UnknownHostException) {
            ErrorViewFactory.buildNoNetworkErrorView(mErrorView, config, onRetryListener);
        } else if (throwable != null && throwable instanceof HttpException) {
            ErrorViewFactory.buildLoadFailErrorView(mErrorView, config, onRetryListener);
        } else if (throwable != null && throwable instanceof ConnectException) {
            ErrorViewFactory.buildLoadFailErrorView(mErrorView, config, onRetryListener);
        } else if (throwable != null && throwable instanceof SocketTimeoutException) {
            ErrorViewFactory.buildLoadFailErrorView(mErrorView, config, onRetryListener);
        } else if (throwable != null && throwable instanceof ServerError) {
            ErrorViewFactory.buildServerErrorView(mErrorView, config, onRetryListener);
        } else {
            ErrorViewFactory.buildLoadFailErrorView(mErrorView, config, onRetryListener);
        }
        //show error view
        mViewAnimator.setDisplayedChildId(R.id.error_view);

    }

    @Override
    public void onLoadingComplete() {
        mViewAnimator.setDisplayedChildId(R.id.content_view);
    }

}
